<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    //判断当前浏览器是否支持websocket
    if(window.WebSocket) {
        //go on
        socket = new WebSocket("ws://127.0.0.1:10/ws");
        //相当于channelReado, ev 收到服务器端回送的消息
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            var obj = JSON.parse(ev.data)
            rt.value = rt.value + "\n" + obj.msg;
            console.log(rt)
        }
 
        //相当于连接开启(感知到连接开启)
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启了.."
            console.log(rt)
        }
 
        //相当于连接关闭(感知到连接关闭)
        socket.onclose = function (ev) {
 
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + "连接关闭了.."
            console.log(rt)
        }
    } else {
        alert("当前浏览器不支持websocket")
    }
 
    //发送消息到服务器
    function send(to, message) {
        var obj = {
            "to": to,
            "msg":message
        }
        sendMsg(1, obj)
    }

    function sendMsg(type, msg) {
        if(!window.socket) { //先判断socket是否创建好
            return;
        }
        if(socket.readyState == WebSocket.OPEN) {
            //通过socket 发送消息
            socket.send(type+"&&"+ JSON.stringify(msg))
        } else {
            alert("连接没有开启");
        }
    }

    function login(username) {
        sendMsg(0, {"username":username})
    }
</script>
    <form onsubmit="return false">
        <input type="text"  name="username"  style="height: 20px; width: 300px" />
        <input type="button" value="登录" onclick="login(this.form.username.value)">
        <p>
        发送给：<input type="text" name="to" style="height: 20px; width: 100px" />
        <p></p>
        <textarea name="message" style="height: 100px; width: 290px"></textarea>
        <input type="button" value="发生消息" onclick="send(this.form.to.value, this.form.message.value)">
        </p>
        <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>